package com.monitor.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class GetDate {

	/**
	 * 获取当前时间转String
	 * @param type
	 * @return
	 */
	public String getDate(String type) {

		SimpleDateFormat formatter = new SimpleDateFormat(type);
		String dateString = formatter.format(new Date());
		formatter = null;
		return dateString;
	}
	
	/**
	 * String格式化
	 * @param dateString
	 * @param type
	 * @return
	 * @throws ParseException 
	 */
	public String getDate0(String dateString, String type, String type0){
		SimpleDateFormat formatter = new SimpleDateFormat(type);
		SimpleDateFormat formatter0 = new SimpleDateFormat(type0);
		Date date = null;;
		try{
			date = formatter.parse(dateString);
		}catch(ParseException e){
			e.printStackTrace();
		}
		dateString = formatter0.format(date);
		formatter = null;
		return dateString;
	}

	/**
	 * Timestamp转String
	 * @param date
	 * @param type
	 * @return
	 */
	public String getDate1(Timestamp date, String type ) {

		SimpleDateFormat formatter = new SimpleDateFormat(type);
		String dateString = formatter.format(date);
		formatter = null;
		return dateString;
	}
	
	/**
	 * String转 Timestamp
	 * @param date
	 * @param type
	 * @return
	 */
	public Timestamp getDate2(String date, String type){
		SimpleDateFormat formatter = new SimpleDateFormat(type);
		Date d = null;
		try{
			d = formatter.parse(date);
		}catch(ParseException e){
			e.printStackTrace();
		}
		Timestamp t = new Timestamp(d.getTime());
		formatter = null;
		return t;
	}
	
	/**
	 * Date转String
	 * @param date
	 * @param type
	 * @return
	 */
	public String getdate3(Date date, String type){
		SimpleDateFormat formatter = new SimpleDateFormat(type);
		String dateString = formatter.format(date);
		formatter = null;
		return dateString;
	}
	
	/**
	 * String转 Date
	 * @param dateString
	 * @param type
	 * @return
	 * @throws ParseException 
	 */
	public Date getdate4(String dateString, String type){
		SimpleDateFormat formatter = new SimpleDateFormat(type);
		Date date = null;
		try{
			date = formatter.parse(dateString);
		}catch(ParseException e){
			e.printStackTrace();
		}
		formatter = null;
		return date;
	}
	
	/**
	 * 获取当前时间转String
	 * @param type
	 * @return
	 */
	public Date getDate6(String type) {
		SimpleDateFormat formatter = new SimpleDateFormat(type);
		String dateString = formatter.format(new Date());
		Date date = null;
		try{
			date = formatter.parse(dateString);
		}catch(ParseException e){
			e.printStackTrace();
		}
		formatter = null;
		return date;
	}
	
	/**
	 * 根据时间获取Integer类型年月yyyyMM
	 * @param date
	 * @param type 0本月  1上月
	 * @return
	 */
	public Integer findYearAndmonth(Date date, int type){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		if(type == 1){
			cal.add(Calendar.MONTH, -1);
		}
		Date newDate = cal.getTime();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMM");
		String dateString = formatter.format(newDate);
		return Integer.valueOf(dateString);
	}
	
	/**
	 * 时间计算
	 * @param date
	 * @param count 加减数量
	 * @param type 0年计算 1月计算 2日计算
	 * @return
	 */
	public Date calculationDate(Date date, int count, int type){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		if(type == 0){
			cal.add(Calendar.YEAR, count);
		}else if(type == 1){
			cal.add(Calendar.MONTH, count);
		}else{
			cal.add(Calendar.DATE, count);
		}
		return cal.getTime();
	}

	public static int daysBetween(String smdate, String bdate){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		long time1 = 0;
		try{
			cal.setTime(sdf.parse(smdate));
			time1 = cal.getTimeInMillis();
			cal.setTime(sdf.parse(bdate));
		}catch(ParseException e){
			e.printStackTrace();
		}
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}
	
	public static void main(String[] args){
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMM");
		Date date = new Date();
		try {
			date = formatter.parse("201701");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		Date newDate = cal.getTime();
		String dateString = formatter.format(newDate);
		System.out.println(Integer.valueOf(dateString));
	}
}
